package com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.event.AdRefreshEvent;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.DynamicSimpleRetrofit;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.DiskLruCacheUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by daniel on 17-12-13.
 */

public class ADConfigs {
    private static List<ADConfigBean> sConfigs;

    public static void upAdConfig(List<ADConfigBean> configs) {
        sConfigs = configs;
    }

    /**
     * @param position 广告的位置:1启动页，2精选插屏，3充值页通栏，4书架收藏，5精选通栏，6阅读页悬浮，7书架上方banner，
     *                 8精选页面下方固定banner，9详情页面下方固定banner，10每章衔接之间广告
     * @return 广告类型:1sdk广告(美生源)，2api广告，3图文广告，4sdk广告(广点通) 此外0表示关闭广告
     */
    @Nullable
    public static ADConfigBean  showAD(String position) {
        ADConfigBean res = null;
        if (sConfigs == null) {
            try {
                Type type = new TypeToken<ArrayList<ADConfigBean>>() {
                }.getType();
                sConfigs = DiskLruCacheUtils.get(Constants.AD_CONFIG, type);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        if (sConfigs != null) {
            for (ADConfigBean item : sConfigs) {
                //不是美生元的广告才显示
                boolean b = TextUtils.equals(item.ad_position, position) && !"1".equals(item.ad_type) && !"2".equals(item.ad_type);
                if (b) {
                    Log.d("evan", " ad_position: " + item.ad_position + "   is_close: " + item.is_close);
                    if (!TextUtils.equals(item.is_close, "1")) {
                        res = item;
                    }
                    break;
                }
            }
        }
        return res;
    }

    public static void updateAdConfig() {
        updateAdConfig(false);
    }

    public static void updateAdConfig(boolean sendEvent) {
        RetrofitHelper helper = RetrofitHelper.getInstance();
        //helper.setRetrofit(new DynamicSimpleRetrofit());
        helper.createService(DynamicSimpleRetrofit.class, ADsService.class).getNewAdsConfig()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<ADConfigBean>>() {
                    @Override
                    public void onResponse(BaseResult<List<ADConfigBean>> adConfigBeanBaseResult, List<ADConfigBean> adConfigBean, Disposable disposable) {
                        for (ADConfigBean item : adConfigBean) {
                            if (!Utils.isEmptyList(item.ad_type_lists)) {
                                item.ad_type = item.ad_type_lists.get(0).ad_type;
                                item.ad_position_id = item.ad_type_lists.get(0).ad_position_id;
                            }
                            //360广告需要拉取广告配置之后初始化充电广告
                            if (TextUtils.equals(item.ad_position, String.valueOf(ADConfigBean.POSITION_CHARGE_LOCK_SCREEN))) {
                                if (!TextUtils.equals(item.is_close, "1")) {
                                    Xutils.init360ChargeSDK(item.ad_position_id);
                                }
                            }
                        }
                        upAdConfig(adConfigBean);
                        if (sendEvent) {
                            EventBus.getDefault().post(new AdRefreshEvent());
                        }
                        DiskLruCacheUtils.put(Constants.AD_CONFIG, adConfigBean);
                    }

                    @Override
                    public void onError(int code, String errDesc) {
                        Log.i("AppAd", "err" + errDesc);
                    }
                });
    }

}
